
/**author:yebinx email:yebinx@21cm.com*/
export default class FlyManager{
    private static _ins:FlyManager;
    constructor(){};

    public flyOneByImgUrl(_url:string,parent,fromX:number,fromY:number,toX:number,toY:number,complete:Function=null,time:number=1000){
        let timg:Laya.Image=new Laya.Image(_url);
        timg.pos(fromX,fromY);
        parent.addChild(timg);
        Laya.Tween.to(timg,{x:toX,y:toY},time,null,new Laya.Handler(this,()=>{
            timg.removeSelf();
            //timg.dispose();
            timg=null;
            if(complete)complete();
        }));
    }
	
	public flyItems(_arr,_parent=null,complete:Function=null){
		let tindex=0;
		for(let i=0;i<_arr.length;i++){
			this.flyItem(_arr[i],_parent,()=>{
				tindex++;
				if(tindex==_arr.length){
					if(complete)complete();
				}
			});
		}
	}
	/**_data {x,y,toX,toY,skin}*/
	public flyItem(_data,_parent=null,complete:Function=null){
		let timg:Laya.Image=new Laya.Image(_data.skin);
		let tparent = _parent?_parent:Laya.stage;
		timg.pos(_data.x,_data.y);
		timg.scaleX=timg.scaleY=0;
		tparent.addChild(timg);
		let tx:number=_data.x+100*Math.cos(Math.random()*360);
		let ty:number=_data.y+100*Math.sin(Math.random()*360);
		Laya.Tween.to(timg,{x:tx,y:ty,scaleX:1,scaleY:1},500,null,new Laya.Handler(this,()=>{
		   Laya.Tween.to(timg,{x:_data.toX,y:_data.toY},1000,null,new Laya.Handler(this,()=>{
				timg.removeSelf();
				timg=null;
				if(complete)complete();
		   }),500);
		}));
		
	}

	public flyOneBySp(_sp:any,toX:number,toY:number,complete:Function=null,time:number=1000){
		Laya.Tween.to(_sp,{x:toX,y:toY},time,null,new Laya.Handler(this,()=>{
            _sp.removeSelf();
            _sp=null;
            if(complete)complete();
        }));
	}

	public flyArrSp(_arr,complete:Function=null){
		let tindex=0;
		for(let i=0;i<_arr.length;i++){
			this.flyOneBySp(_arr[i].sp,_arr[i].x,_arr[i].y,()=>{
				tindex++;
				if(tindex==_arr.length){
					if(complete)complete();
				}
			});
		}
	}

    public static getIns():FlyManager{
        if(!this._ins)this._ins=new FlyManager();
        return this._ins;
    }
}